class Solution {
    int ret;
    int depth(TreeNode*root)
    {
        if(root==nullptr) return 0;
        int l=depth(root->left);
        int r=depth(root->right);
        ret=max(ret,l+r+1);
        return max(l,r)+1;
    }
public:
    int diameterOfBinaryTree(TreeNode* root) {
        ret=1;
        depth(root);
        return ret-1;
    }
};